//
//  NSString+Comma.m
//  饼图~demo
//
//  Created by 石虎 on 2017/10/25.
//  Copyright © 2017年 石虎. All rights reserved.
//

#import "NSString+Comma.h"

@implementation NSString (Comma)

+(NSString*)strmethodComma:(NSString*)str
{
    
    NSString *intStr;
    
    NSString *floStr;
    
    if ([str containsString:@"."]) {
        
        NSRange range = [str rangeOfString:@"."];
        
        floStr = [str substringFromIndex:range.location];
        
        intStr = [str substringToIndex:range.location];
        
        
    }else{
        
        floStr = @"";
        
        intStr = str;
        
    }
    
    
    if (intStr.length <=3) {
        
        return [intStr stringByAppendingString:floStr];
        
    }else{
        
        NSInteger length = intStr.length;
        
        NSInteger count = length/3;
        
        NSInteger y = length%3;
        
        
        NSString *tit = [intStr substringToIndex:y] ;
        
        NSMutableString *det = [[intStr substringFromIndex:y] mutableCopy];
        
        
        for (int i =0; i < count; i ++) {
            
            NSInteger index = i + i *3;
            
            [det insertString:@","atIndex:index];
            
        }
        
        if (y ==0) {
            
            det = [[det substringFromIndex:1]mutableCopy];
            
        }
        
        intStr = [tit stringByAppendingString:det];
        
        return [intStr stringByAppendingString:floStr];
        
    }
}

@end
